<template>
    <f7-page @page:beforein="pageBeforeIn">
        <f7-navbar>
            <f7-nav-left>
                <f7-link @click="back">
                    <f7-icon icon="icon-back" style="margin-left:15px;"></f7-icon>
                </f7-link>
            </f7-nav-left>
            <f7-nav-title title="批次详情"></f7-nav-title>
        </f7-navbar>
        <f7-block strong inset>
            <f7-row>
                <f7-col width="30">
                    <span>批次用途</span>
                </f7-col>
                <f7-col width="70">
                    <span v-text="checkQualityBatch.batchPurpose"></span>
                </f7-col>
            </f7-row>
            <f7-row>
                <f7-col width="30">
                    <span>批次名称</span>
                </f7-col>
                <f7-col width="70">
                    <span v-text="checkQualityBatch.name"></span>
                </f7-col>
            </f7-row>
            <f7-row>
                <f7-col width="30">
                    <span>所属标段</span>
                </f7-col>
                <f7-col width="70" v-if="checkQualityBatch.bidSectionId != null">
                    <span v-text="showBidSections"></span>
                </f7-col>
            </f7-row>
            <f7-row>
                <f7-col width="30">
                    <span>检查人</span>
                </f7-col>
                <f7-col width="70">
                    <f7-row v-for="(regist, i) in checkQualityBatch.regists" v-if="i % 3 == 0" :key="regist">
                        <f7-col width="33">
	                        <span v-text="$store.state.global.userMap[checkQualityBatch.regists[i]].realname">
	                        </span>
                        </f7-col>
                        <f7-col width="33">
	                        <span v-if="i + 1 < checkQualityBatch.regists.length"
	                            v-text="$store.state.global.userMap[checkQualityBatch.regists[i + 1]].realname">
	                        </span>
                        </f7-col>
                        <f7-col width="33">
	                        <span v-if="i + 2 < checkQualityBatch.regists.length"
	                            v-text="$store.state.global.userMap[checkQualityBatch.regists[i + 2]].realname">
	                        </span>
                        </f7-col>
                    </f7-row>
                </f7-col>
            </f7-row>
            <f7-row>
                <f7-col width="30">
                    <span>整改期限</span>
                </f7-col>
                <f7-col width="70">
                    <span v-text="checkQualityBatch.repairDeadline"></span>
                </f7-col>
            </f7-row>
            <f7-row>
                <f7-col width="30">
                    <span>抄送人</span>
                </f7-col>
                <f7-col width="70">
                    <f7-row v-for="(cc, i) in checkQualityBatch.ccs" v-if="i % 3 == 0" :key="cc">
                        <f7-col width="33">
	                        <span v-text="$store.state.global.userMap[checkQualityBatch.ccs[i]].realname">
	                        </span>
                        </f7-col>
                        <f7-col width="33">
	                        <span v-if="i + 1 < checkQualityBatch.ccs.length"
	                            v-text="$store.state.global.userMap[checkQualityBatch.ccs[i + 1]].realname">
	                        </span>
                        </f7-col>
                        <f7-col width="33">
	                        <span v-if="i + 2 < checkQualityBatch.ccs.length"
	                            v-text="$store.state.global.userMap[checkQualityBatch.ccs[i + 2]].realname">
	                        </span>
                        </f7-col>
                    </f7-row>
                </f7-col>
            </f7-row>
            <f7-row>
                <f7-col width="30">
                    <span>负责人</span>
                </f7-col>
                <f7-col width="70" v-if="checkQualityBatch.inChargeBy != null">
                    <span v-text="$store.state.global.userMap[checkQualityBatch.inChargeBy].realname"></span>
                </f7-col>
            </f7-row>
        </f7-block>
        <f7-fab v-if="isShowClose" @click="checkQualityBatchClose"
            position="left-bottom" slot="fixed" text="关闭" color="yellow">
            <f7-icon ios="material:close" md="material:close"></f7-icon>
        </f7-fab>
        <f7-fab v-if="isShowEdit" @click="checkQualityBatchEdit"
            position="right-bottom" slot="fixed" text="修改" color="blue">
            <f7-icon ios="material:edit" md="material:edit"></f7-icon>
        </f7-fab>
    </f7-page>
</template>

<script>
import networkEvent from '@/mixins/networkEvent';
import back from '@/mixins/back.js';
import Common from "@/utils/Common.js";
import DateUtil from "@/utils/DateUtil.js";
import events from 'events';
import store from '@/store';
import { getUser, setUser, clearUser } from '@/utils/auth';
import { getStore, setStore, clearStore } from '@/utils/storage';
import keys from '@/utils/rememberKeys';
import request from '@/utils/request';
import db from '@/utils/db';

export default {
    mixins: [networkEvent, back],
    store: store,
    data: function() {
    	return {
    		batchId: null,
    		checkQualityBatch: {}
    	}
    },
    methods: {
    	pageBeforeIn: function(e, page) {
            var self = this;
            self.batchId = self.$f7route.query.batchId;
            self.$f7.preloader.show();
            let sql = "\
                select t.*, p.regists, q.ccs \
                from tb_checkquality_batch t \
                left join ( \
                    select batch_id, \
                        case when GROUP_CONCAT(user_id) is null then '[]' else '[' || GROUP_CONCAT('\"' || user_id || '\"') || ']' end as regists \
                    from tb_batch_user \
                    where batch_id = ? \
                ) p \
                on t.id = p.batch_id \
                left join ( \
                	select batch_id, \
                        case when GROUP_CONCAT(user_id) is null then '[]' else '[' || GROUP_CONCAT('\"' || user_id || '\"') || ']' end as ccs \
                    from tb_batch_cc \
                    where batch_id = ? \
                ) q \
                on t.id = q.batch_id \
                where id = ? \
            ";
            Common.executeSql(sql, [self.batchId, self.batchId, self.batchId]).then((result) => {
            	let rs = result.rs;
                let row = rs.rows.item(0);
                let checkQualityBatch = {};
                checkQualityBatch.id = row.id;
                checkQualityBatch.batchPurpose = row.batch_purpose;
                checkQualityBatch.batchType = row.batch_type;
                checkQualityBatch.name = row.name;
                checkQualityBatch.repairDeadline = row.repair_deadline;
                checkQualityBatch.bidSectionId = row.bid_section_id;
                checkQualityBatch.inChargeBy = row.in_charge_by;
                checkQualityBatch.isClose = row.isClose;
                checkQualityBatch.regists = 
                	Common.strToJson(row.regists) || [];
                checkQualityBatch.ccs = 
                	Common.strToJson(row.ccs) || [];
                self.checkQualityBatch = checkQualityBatch;
                self.$f7.preloader.hide();
            }).catch((error) => {
            	console.error(error);
            	self.$f7.preloader.hide();
            });
    	},
    	checkQualityBatchClose: function() {
    		var self = this;
            self.$f7.dialog.confirm('批次关闭后不能在APP移动质检中显示，仅可在PC端统计报表中查看', "是否关闭批次？",
                function() {
            	    self.$f7.preloader.show();
            	    request({
            	    	url: '/api/checkQualityBatch/close',
            	    	method: 'post',
            	    	data: {
            	    		batchId: self.batchId
            	    	}
            	    }).then(data => {
            	    	let batch = data.element;
            	    	return db.update('tb_checkquality_batch', [batch]);
            	    }).then(() => {
                        self.$f7.dialog.alert('', '关闭批次成功！');
                        self.$f7.preloader.hide();
            	    }).catch(error => {
            	    	console.error(error);
            	    	self.$f7.dialog.alert('', '关闭批次失败');
            	    	self.$f7.preloader.hide();
            	    });
                },
                function() {}
            );
    	},
    	checkQualityBatchEdit: function() {
    		var self = this;
            var router = this.$f7router.navigate('/Workbench/LocaleCheck/BatchModify', {
            	props: {
            		batch: this.checkQualityBatch
            	}
            });
    	}
    },
    computed: {
        showBidSections: function() {
            var str = "";
            var bidSection = this.$store.state.global.bidSectionMap[this.checkQualityBatch.bidSectionId];
            str += bidSection.bid_section_name;
            str += "(";
            var project = this.$store.state.global.projectMap[bidSection.project_id];
            str += project.project_name;
            str += ")";
            return str;
        },
        isShowClose: function() {
        	if(this.isOnline) {
	        	if(this.checkQualityBatch.inChargeBy == getUser().userId) {
	        		return true;
	        	}
        	}
        	return false;
        },
        isShowEdit: function() {
            if(this.isOnline) {
                if(this.checkQualityBatch.inChargeBy == getUser().userId) {
                    return true;
                }
            }
            return false;
        }
    }
}
</script>

<style scoped>

</style>